from deck import Deck
import config

class Dealer:
	u"Krupier"
	def __init__(self):
		#if len(cards) != 7:
		#	raise TypeError, "%d cards given, not 7" % len(cards)
		""
	def CheckPlayers(self, table):
		if len(table.players) < 2:
			return False #must be at least 2 players
		for i in range(len(table.players)):
			if table.players[i].ready == False:
				return False
		table.Broadcast("All %d players are ready. %s begins" % (len(table.players), table.players[table.current_player].name))
		return True
	def NextPlayer(self, table):
		players = len(table.players)
		active = table.current_player
		active+=1
		if active >= players:
			active = 0
		table.current_player = active
		if not table.ActivePlayers():
			table.NextPhase()
	def Judge(self, players, cards):
		p = players
		c = cards
		for i in range(len(p)):
			p[i].PickStrongestHand(c)
			p[i].Text("Your strongest set is: %s (%s)" % (p[i].strongest_hand, p[i].set_name))
		winners = sorted(p, key=lambda p: p.strength, reverse=True)
		return winners
	def RetrieveBlinds(self, table):
		"Take the blinds from our players"
		name=[]
		for i in range(len(table.players)):
			if table.players[i].blind == 1:
				table.players[i].SmallBlind(table)
				name.append(table.players[i].name)
		for i in range(len(table.players)):
			if table.players[i].blind == 2:
				table.players[i].BigBlind(table)
				name.append(table.players[i].name)
		table.Broadcast("Blinds: %s-small, %s-big" % (name[0], name[1]))
	def GetSets(self, cards):
	        if len(cards) != 7:
                        raise TypeError, "%d cards given, not 7" % len(cards)
		c = cards
		sets = []
		sets.append([c[0], c[1], c[2], c[3], c[4]])
		sets.append([c[1], c[2], c[3], c[4], c[5]])
		sets.append([c[2], c[3], c[4], c[5], c[6]])
		sets.append([c[0], c[2], c[3], c[4], c[5]])
		sets.append([c[0], c[3], c[4], c[5], c[6]])
                sets.append([c[0], c[1], c[3], c[4], c[5]])
		sets.append([c[0], c[1], c[4], c[5], c[6]])
		sets.append([c[0], c[1], c[2], c[4], c[5]])
		sets.append([c[0], c[1], c[2], c[5], c[6]])
		sets.append([c[0], c[1], c[2], c[3], c[5]])
		sets.append([c[0], c[1], c[2], c[3], c[6]])
		return sets
	def ScoreSet(self, cards):
		u"Sila setu"	#we should modify it to be able to tell ppl what set do they have	
		set_name = ""
        	De = Dealer()
        	if De._Dealer__IsSameColor(cards):
                	if De._Dealer__Stairs(cards):
				hand = De._Dealer__Stairs(cards)
				score = hand * 1000000000000000
				set_name = "royal flush"
                	else:
                        	hand = De._Dealer__IsSameColor(cards)
				score = hand * 1000000000
				set_name = "color"
        	elif De._Dealer__FourOfAKind(cards):
                	hand = De._Dealer__FourOfAKind(cards)
			score = hand[0] * 1000000000000 + hand[1]
			set_name = "four of a kind"
        	elif De._Dealer__FullHouse(cards):
                	hand = De._Dealer__FullHouse(cards)
			score = hand[0] * 10000000000 + hand[1]
			set_name = "full house"
        	elif De._Dealer__Stairs(cards):
                	hand = De._Dealer__Stairs(cards)
			score = hand * 100000000
			set_name = "stairs"
        	elif De._Dealer__ThreeOfAKind(cards):
                	hand = De._Dealer__ThreeOfAKind(cards)
			score = hand[0] * 10000000 + hand[1] * 100 + hand[2]
			set_name = "three of a kind"
        	elif De._Dealer__TwoPair(cards):
                	hand = De._Dealer__TwoPair(cards)
			score = hand[0] * 1000000 + hand[1] * 100 + hand[2]
			set_name = "two pair"
        	elif De._Dealer__Pair(cards):
                	hand = De._Dealer__Pair(cards)
			score = hand[0] * 100000 + hand[1] * 100 + hand[2] * 10 + hand[3]
			set_name = "pair"
        	elif De._Dealer__HighCards(cards):
                	hand = De._Dealer__HighCards(cards)
			score = hand[0] * 10000 + hand[1] * 1000 + hand[2] * 100 + hand[3] * 10 + hand[4]
			set_name = "high cards"
		if config.debug:
			print "DEBUG %s = %d" % (cards, score)
		return score, set_name
	def __IsSameColor(self, cards):
		u"Karty maja ten sam kolor"
		color=cards[0][cards[0].find("-"):]
		for i in range(1, len(cards)):
			if color != cards[i][cards[i].find("-"):]:
				return False
		cards = Deck().Digitize(cards)
		cards.sort()
		return cards[-1]
	def __Stairs(self, cards):
		u"Karty to schodki(strit)"
		cards = Deck().Digitize(cards)
		cc = cards.count
		if cc(2) and cc(3) and cc(4) and cc(5) and cc(14):
			return 5
		cards.sort()
		for i in range(len(cards)-1):
			if cards[i]+1 != cards[i+1]:
				return False
		return cards[-1]
	def __FourOfAKind(self, cards):
		cards = Deck().Digitize(cards)
		cards.sort()
		if cards.count(cards[0]) != 4 and cards.count(cards[0]) != 1:
			return False
		if cards.count(cards[0]) == 1:
			if cards.count(cards[-1]) == 4:
				return [cards[-1], cards[0]]
			else:
				return False
		if cards.count(cards[0]) == 4:
			if cards.count(cards[-1]) == 1:
				return [cards[0], cards[-1]]
		return False
        def __FullHouse(self, cards):
                cards = Deck().Digitize(cards)
                cards.sort()
                if cards.count(cards[0]) != 3 and cards.count(cards[0]) != 2:
                        return False
                if cards.count(cards[0]) == 2:
                        if cards.count(cards[-1]) == 3:
                                return [cards[-1], cards[0]]
                        else:
                                return False
                if cards.count(cards[0]) == 3:
                        if cards.count(cards[-1]) == 2:
                                return [cards[0], cards[-1]]
                return False
	def __ThreeOfAKind(self, cards):
		cards = Deck().Digitize(cards)
		cards.sort()
		if cards.count(cards[0]) != 3 and cards.count(cards[1]) != 3 and cards.count(cards[2]) != 3:
			return False
		if cards.count(cards[0]) == 3:
			return [cards[0], cards[-1], cards[-2]]
		if cards.count(cards[1]) == 3:
			return [cards[1], cards[-1], cards[0]]
		if cards.count(cards[2]) == 3:
			return [cards[2], cards[1], cards[0]]
	def __TwoPair(self, cards):
		cards = Deck().Digitize(cards)
		cards.sort()
		if cards.count(cards[0]) == 1 and cards.count(cards[1]) == 2 and cards.count(cards[3]) == 2:
			return [cards[3], cards[1], cards[0]]
		if cards.count(cards[0]) == 2 and cards.count(cards[2]) == 1 and cards.count(cards[3]) == 2:
			return [cards[3], cards[0], cards[2]]
		if cards.count(cards[0]) == 2 and cards.count(cards[2]) == 2 and cards.count(cards[4]) == 1:
			return [cards[2], cards[0], cards[4]]
		return False
	def __Pair(self, cards):
		cards = Deck().Digitize(cards)
		cards.sort()
		pair = 0
		for i in range(len(cards)):
			if cards.count(cards[i]) == 2:
				pair = cards[i]
		if pair == 0:
			return False
		cards.remove(pair)
		cards.append(pair)
		cards.reverse()
		return cards
	def __HighCards(self, cards):
		cards = Deck().Digitize(cards)
		cards.sort()
		cards.reverse()
		return cards
